package com.itheima.controller;

import com.itheima.annotation.AopAnnotation;
import com.itheima.pojo.Emp;
import com.itheima.dto.EmpQueryParam;
import com.itheima.pojo.PageResult;
import com.itheima.pojo.Result;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private EmpService empService;

    @PutMapping
    @AopAnnotation
    public Result update(@RequestBody Emp emp){
        empService.update(emp);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getInfo(@PathVariable Integer id){
        Emp emp = empService.getInfo(id);
        return Result.success(emp);
    }

    @DeleteMapping
    @AopAnnotation
    public Result deleteById(@RequestParam List<Integer> ids){
        empService.delete(ids);
        return Result.success();
    }

    @GetMapping("/list")
    public Result findAll(){
        List<Emp> list = empService.finAll();
        return Result.success(list);
    }

    @GetMapping
    public Result page(EmpQueryParam empQueryParam){
        PageResult pageResult = empService.page(empQueryParam);
        return Result.success(pageResult);
    }

    @PostMapping
    @AopAnnotation
    public Result save(@RequestBody Emp emp){
        empService.save(emp);
        return Result.success();
    }
}
